package com.ruoyi.cs.event.customeroffer.service;

import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.cs.event.customeroffer.domain.CustomerOffer;
import com.ruoyi.cs.event.customeroffer.domain.CustomerOfferList;
import com.ruoyi.cs.event.customeroffer.mapper.CustomerOfferMapper;
import com.ruoyi.utils.ID.IdUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 客户报价
 * 业务层执行
 * @author Drj 2021/6/28
 */
@Service
public class CustomerOfferServiceImpl implements ICustomerOfferService {
    @Autowired
    private CustomerOfferMapper customerOfferMapper;
    @Autowired
    private IdUtil idUtil;

    /**
     * 客户报价查询
     *
     * @param customerOffer 查询条件
     * @return
     */
    @Override
    public List<CustomerOffer> selectCustomerOfferList(CustomerOffer customerOffer) {
        List<CustomerOffer> customerOffers = customerOfferMapper.selectCustomerOfferList(customerOffer);
//        for (int i = 0; i < customerOffers.size(); i++) {
//            customerOffers.get(i).setCustomerOfferLists(customerOfferMapper.selectCustomerOfferListListById(customerOffers.get(i).getCustomerQuotaId()));
//        }
        return customerOffers;
    }

    /**
     * 客户报价根据编号查询
     *
     * @param customerQuotaId
     * @return
     */
    @Override
    public CustomerOffer selectCustomerOfferById(String customerQuotaId) {
        return customerOfferMapper.selectCustomerOfferById(customerQuotaId);
    }

    /**
     * 新增客户报价
     *
     * @param customerOffer
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int insertCustomerOffer(CustomerOffer customerOffer) throws Exception{
        if (StringUtils.isNotNull(customerOfferMapper.selectCustomerOfferById(customerOffer.getCustomerQuotaId()))){
            //存在
            return 0;
        }
        //不存在
        String customerQuotaId = idUtil.createCustomerQuotaId();
        customerOffer.setCustomerQuotaId(customerQuotaId);
        customerOfferMapper.insertCustomerOffer(customerOffer);
        for (int i = 0; i < customerOffer.getCustomerOfferLists().size(); i++) {
            customerOffer.getCustomerOfferLists().get(i).setCustomerQuotaId(customerQuotaId);
            customerOfferMapper.insertCustomerOfferList(customerOffer.getCustomerOfferLists().get(i));
        }
        return 1;
    }

    /**
     * 更新客户报价
     *
     * @param customerOffer
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int updateCustomerOffer(CustomerOffer customerOffer)throws Exception {
        if (!StringUtils.isNotNull(customerOfferMapper.selectCustomerOfferById(customerOffer.getCustomerQuotaId()))){
            //不存在
            return 0;
        }
        customerOfferMapper.updateCustomerOffer(customerOffer);
        //现有数据库内的
        List<CustomerOfferList> CustomerOfferListsOld = customerOfferMapper.selectCustomerOfferListListById(customerOffer.getCustomerQuotaId());
        //更新部分
        List<CustomerOfferList> CustomerOfferListsNew = customerOffer.getCustomerOfferLists();
        for (int i = 0; i < CustomerOfferListsNew.size(); i++) {
            //查询数据库此条客户报价详细是否存在
            CustomerOfferList customerOfferList = customerOfferMapper.selectCustomerOfferListById(CustomerOfferListsNew.get(i));
            if (StringUtils.isNotNull(customerOfferList)){
                //存在
                for (int j = 0; j < CustomerOfferListsOld.size(); j++) {
                    if (CustomerOfferListsOld.get(j).getProduceId().equals(customerOfferList.getProduceId())){
                        //移除旧有中相同部分
                        CustomerOfferListsOld.remove(i);
                        break;
                    }
                }
                //更新
                customerOfferMapper.updateCustomerOfferList(CustomerOfferListsNew.get(i));
            }else {
                //不存在，新增
                customerOfferMapper.insertCustomerOfferList(CustomerOfferListsNew.get(i));
            }
        }
        for (int i = 0; i < CustomerOfferListsOld.size(); i++) {
            customerOfferMapper.deleteCustomerOfferListById(CustomerOfferListsOld.get(i));
        }
        return 1;
    }

    /**
     * 删除客户报价
     * 仅修改删除标志
     *
     * @param customerOffer
     * @return
     */
    @Override
    public int updateCustomerOfferDeleteFlag(CustomerOffer customerOffer) {
        return customerOfferMapper.updateCustomerOfferDeleteFlag(customerOffer);
    }
}
